<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px">
<!--      <el-form-item label="id" prop="id">-->
<!--        <el-input-->
<!--          v-model="queryParams.id"-->
<!--          placeholder="请输入id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="组织编码" prop="orgnumber" label-width="68px">
        <el-input
          v-model="queryParams.orgnumber"
          placeholder="请输入组织编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="父组织编码" prop="parentorgnumber">-->
<!--        <el-input-->
<!--          v-model="queryParams.parentorgnumber"-->
<!--          placeholder="请输入父组织编码"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="组织名称" prop="orgname" label-width="68px">
        <el-input
          v-model="queryParams.orgname"
          placeholder="请输入组织名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="中转仓编码" prop="transferwhnumber">-->
<!--        <el-input-->
<!--          v-model="queryParams.transferwhnumber"-->
<!--          placeholder="请输入中转仓编码"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="中转仓名称" prop="transferwhname">-->
<!--        <el-input-->
<!--          v-model="queryParams.transferwhname"-->
<!--          placeholder="请输入中转仓名称"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="级次" prop="level">-->
<!--        <el-input-->
<!--          v-model="queryParams.level"-->
<!--          placeholder="请输入级次"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="长编码" prop="longnumber">-->
<!--        <el-input-->
<!--          v-model="queryParams.longnumber"-->
<!--          placeholder="请输入长编码"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="一级组织编码" prop="firstorgnumber">
        <el-input
          v-model="queryParams.firstorgnumber"
          placeholder="请输入一级组织编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="一级组织名称" prop="firstorgname">
        <el-input
          v-model="queryParams.firstorgname"
          placeholder="请输入一级组织名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否分子公司" prop="issub">
        <el-select v-model="queryParams.issub" placeholder="请选择是否分子公司" clearable>
          <el-option
            v-for="dict in dict.type.yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['settlementCenter/basedata:orgunit:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-sort"
          size="mini"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="orgunitList"
      row-key="orgnumber"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="组织编码" align="left" prop="orgnumber" />
      <el-table-column label="级次" align="center" prop="level" />
      <el-table-column label="父组织编码" align="center" prop="parentorgnumber" />
      <el-table-column label="组织名称" align="center" prop="orgname" />
      <el-table-column label="一级组织编码" align="center" prop="firstorgnumber" />
      <el-table-column label="一级组织名称" align="center" prop="firstorgname" />
      <el-table-column label="中转仓编码" align="center" prop="transferwhnumber" />
      <el-table-column label="中转仓名称" align="center" prop="transferwhname" />
      <el-table-column label="长编码" align="center" prop="longnumber" />
      <el-table-column label="是否分子公司" align="center" prop="issub">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yes_no" :value="scope.row.issub"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['settlementCenter/basedata:orgunit:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['settlementCenter/basedata:orgunit:add']"
          >新增</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['settlementCenter/basedata:orgunit:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改结算组织对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="组织编码" prop="orgnumber">
          <el-input v-model="form.orgnumber" placeholder="请输入组织编码" />
        </el-form-item>
        <el-form-item label="父组织编码" prop="parentorgnumber">
          <treeselect v-model="form.parentorgnumber" :options="orgunitOptions" :normalizer="normalizer" placeholder="请选择父组织编码" />
        </el-form-item>
        <el-form-item label="组织名称" prop="orgname">
          <el-input v-model="form.orgname" placeholder="请输入组织名称" />
        </el-form-item>
        <el-form-item label="中转仓编码" prop="transferwhnumber">
          <el-input v-model="form.transferwhnumber" placeholder="请输入中转仓编码" />
        </el-form-item>
        <el-form-item label="中转仓名称" prop="transferwhname">
          <el-input v-model="form.transferwhname" placeholder="请输入中转仓名称" />
        </el-form-item>
        <!--        <el-form-item label="级次" prop="level">-->
        <!--          <el-input v-model="form.level" placeholder="请输入级次" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="长编码" prop="longnumber">-->
        <!--          <el-input v-model="form.longnumber" placeholder="请输入长编码" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="一级组织编码" prop="firstorgnumber">-->
        <!--          <el-input v-model="form.firstorgnumber" placeholder="请输入一级组织编码" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="一级组织名称" prop="firstorgname">-->
        <!--          <el-input v-model="form.firstorgname" placeholder="请输入一级组织名称" />-->
        <!--        </el-form-item>-->
        <el-form-item label="是否分子公司" prop="issub">
          <el-select v-model="form.issub" placeholder="请选择是否分子公司">
            <el-option
              v-for="dict in dict.type.yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="Number(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listOrgunit, getOrgunit, delOrgunit, addOrgunit, updateOrgunit } from "@/api/settlementCenter/basedata/orgunit";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";

  export default {
    name: "Orgunit",
    dicts: ['yes_no'],
    components: {
      Treeselect
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 显示搜索条件
        showSearch: true,
        // 结算组织表格数据
        orgunitList: [],
        // 结算组织树选项
        orgunitOptions: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 是否展开，默认全部展开
        isExpandAll: false,
        // 重新渲染表格状态
        refreshTable: true,
        // 查询参数
        queryParams: {
          id: null,
          orgnumber: null,
          parentorgnumber: null,
          orgname: null,
          transferwhnumber: null,
          transferwhname: null,
          level: null,
          longnumber: null,
          firstorgnumber: null,
          firstorgname: null,
          issub: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          orgnumber: [
            { required: true, message: "组织编码不能为空", trigger: "blur" }
          ],
          parentorgnumber: [
            { required: true, message: "父组织编码不能为空", trigger: "blur" }
          ],
          orgname: [
            { required: true, message: "组织名称不能为空", trigger: "blur" }
          ],
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询结算组织列表 */
      getList() {
        this.loading = true;
        listOrgunit(this.queryParams).then(response => {
          this.orgunitList = this.handleTree(response.data, "orgnumber", "parentorgnumber");
          this.loading = false;
        });
      },
      /** 转换结算组织数据结构 */
      normalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.orgnumber,
          label: node.orgname,
          children: node.children
        };
      },
      /** 查询结算组织下拉树结构 */
      getTreeselect() {
        listOrgunit().then(response => {
          this.orgunitOptions = [];
          const data = { orgnumber: 0, orgname: '顶级节点', children: [] };
          data.children = this.handleTree(response.data, "orgnumber", "parentorgnumber");
          this.orgunitOptions.push(data);
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          parentid: null,
          orgnumber: null,
          parentorgnumber: null,
          orgname: null,
          transferwhnumber: null,
          transferwhname: null,
          level: null,
          longnumber: null,
          firstorgnumber: null,
          firstorgname: null,
          issub: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd(row) {
        this.reset();
        this.getTreeselect();
        if (row != null && row.orgnumber) {
          this.form.parentorgnumber = row.orgnumber;
        } else {
          this.form.parentorgnumber = 0;
        }
        this.open = true;
        this.title = "添加结算组织";
      },
      /** 展开/折叠操作 */
      toggleExpandAll() {
        this.refreshTable = false;
        this.isExpandAll = !this.isExpandAll;
        this.$nextTick(() => {
          this.refreshTable = true;
        });
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.getTreeselect();
        if (row != null) {
          this.form.parentorgnumber = row.orgnumber;
        }
        getOrgunit(row.id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改结算组织";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateOrgunit(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addOrgunit(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        this.$modal.confirm('是否确认删除结算组织编号为"' + row.id + '"的数据项？').then(function() {
          return delOrgunit(row.id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      }
    }
  };
</script>
